﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using StyleMVVM.Data;
using StyleMVVM.LanguageExtensions;
#if NETFX_CORE
using Windows.ApplicationModel.Resources;

#endif

namespace StyleMVVM.Validation.Impl.Fluent
{
	public class FluentRuleCollection<T> : IFluentRuleCollection<T>
	{
		private readonly IReflectionService reflectionService;
		private readonly List<FluentRule<T>> fluentRules = new List<FluentRule<T>>();
		private readonly List<string> validationProperties = new List<string>();

		public FluentRuleCollection(IReflectionService reflectionService)
		{
			this.reflectionService = reflectionService;
		}

		public IFluentRule<T> AddRule(string ruleName)
		{
			FluentRule<T> newRule = new FluentRule<T>(reflectionService, ruleName);

			fluentRules.Add(newRule);

			return newRule;
		}

		public void ValidateProperty(Expression<Func<T, object>> property)
		{
			validationProperties.Add(
				reflectionService.GetPropertyAccessPath(property));
		}

		public IEnumerable<IFluentRule<T>> AllRules()
		{
#if NETFX_CORE
			ResourceLoader fluentResources =
				new ResourceLoader("StyleMVVM.CSharp/FluentResources");
#else
			ResourceLoader fluentResources =
				new ResourceLoader();
#endif

			foreach (FluentRule<T> fluentRule in fluentRules)
			{
				fluentRule.Compile(fluentResources);
			}

			return fluentRules;
		}

		public IEnumerable<string> AllProperties()
		{
			return validationProperties;
		}
	}
}